package cn.o.app.adapter;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

@SuppressWarnings("unchecked")
public abstract class OItemView<DATA_ITEM> extends RelativeLayout implements
		IItemView<DATA_ITEM> {
	protected int mPosition = -1;

	protected DATA_ITEM mDataProvider;

	protected OAdapter<DATA_ITEM> mAdapter;

	public OItemView(Context context) {
		super(context);
	}

	public OItemView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public OItemView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public void setAdapter(OAdapter<DATA_ITEM> adapter) {
		mAdapter = adapter;
	}

	public OAdapter<DATA_ITEM> getAdapter() {
		return mAdapter;
	}

	public int getPosition() {
		return mPosition;
	}

	public void setPosition(int position) {
		mPosition = position;
	}

	public DATA_ITEM getDataProvider() {
		return mDataProvider;
	}

	public void setDataProvider(DATA_ITEM dataProvider) {
		mDataProvider = dataProvider;
	}

	public void notifyDataSetChanged() {
		if (this.mAdapter == null) {
			return;
		}
		this.mAdapter.notifyDataSetChanged();
	}

	public void setContentView(View view) {
		this.removeAllViews();
		this.addView(view);
	}

	public void setContentView(int layoutResID) {
		this.removeAllViews();
		LayoutInflater.from(this.getContext()).inflate(layoutResID, this);
	}

	public <T extends View> T findViewById(int id, Class<T> viewClass) {
		View v = findViewById(id);
		if (v != null) {
			if (viewClass != null) {
				if (viewClass.isInstance(v)) {
					return (T) v;
				} else {
					return null;
				}
			}
			return (T) v;
		}
		return null;
	}

	@Override
	public View toView() {
		return this;
	}

}
